package com.bankLogisticsSys.controller;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bankLogisticsSys.util.WeixinUtil;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.weixin.sdk.api.AccessTokenApi;
import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.weixin.sdk.api.ApiResult;
import com.jfinal.weixin.sdk.api.TagApi;
import com.jfinal.weixin.sdk.jfinal.ApiController;
import com.jfinal.weixin.sdk.utils.HttpUtils;
import com.jfinal.weixin.sdk.utils.JsonUtils;


//用户标签控制器
public class TagApiController extends ApiController {

	public ApiConfig getApiConfig() {
		return WeixinUtil.getApiConfig();
	}

	/**
	 * 创建标签
	 * tagid:100 银行
	 * tagid:101 邮政揽投员
	 * tagid:102103 测试用
	 * tagid:104 邮政网点
	 * tagid:105 银行总行
	 * @throws UnsupportedEncodingException 
	 */
	public void createTag() throws UnsupportedEncodingException{
		String name = URLDecoder.decode(getPara(0),"UTF-8");;
		ApiResult apiResult = TagApi.create(name);
		renderText(apiResult.getJson());
	}
	
	/**
	 * 删除标签
	 */
	public void deleteTag(){
		int id = getParaToInt(0,0);
		ApiResult apiResult = TagApi.delete(id);
		renderText(apiResult.getJson());
	}
	
	/**
	 * 批量为用户打标签
	 * @param i
	 * 1:银行
	 * 2:邮政
	 */
	public void batchAddTag(){
		int i = getParaToInt(0, 1);
		List<String> openidList = Db.query("select openid from userinfo where role = ?", i);
		int tagid;
		if(i == 1){
			tagid = 100; 
		}
		else if(i == 2){
			tagid = 101;
		}else{
			renderText("error:错误的标签类型");
			return;
		}
		ApiResult apiResult = TagApi.batchAddTag(tagid, openidList);
		renderText(apiResult.getJson());
	}
	
	/**
	 * 获取已经创建的标签
	 */
	public void getTagList(){
		ApiResult apiResult = TagApi.get();
		renderText(apiResult.getJson());
	}
	/**
	 * 获取某个标签下的用户列表
	 * url传参为tagid,默认为银行的tagid
	 */
	public void getUserList(){
		int tagId = getParaToInt(0, 100);
		ApiResult apiResult = TagApi.getUser(tagId);
		renderText(apiResult.getJson());
	}
	/**
	 * 批量为用户打标签(list size超过50)
	 * @param openidList 用户唯一标识符openid的列表
	 * @param to_tagid 分组id
	 * @return ApiResult
	 */
	public static List<ApiResult> memberBatchUpdate(List<String> openidList, int to_tagid) {
		int size = openidList.size();
		int page;
		List<String> sList;
		List<ApiResult> returnList = new ArrayList<ApiResult>();
		if(size%50 !=0){
			page = size /40 +1;
		}else{
			page = size/40;
		}
		for(int i=0; i<page; i++){
			sList = openidList.subList(i*40, ((i+1)*40-1));
			returnList.add(TagApi.batchAddTag(to_tagid, sList));	
		}
		return returnList;
	}
	
	private static String userBatchUpdateUrl = "https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token=";
	
	/**
	 * 为单一用户打标签
	 * @param openid 用户唯一标识符openid
	 * @param to_tagid 分组id
	 * @return ApiResult
	 */
	public static ApiResult userBatchUpdate(String openid, int to_tagid) {
		String url = userBatchUpdateUrl + AccessTokenApi.getAccessTokenStr();
		
		Map<String, Object> mapData = new HashMap<String, Object>();
		ArrayList<String> openidList = new ArrayList<String>();
		openidList.add(openid);
		mapData.put("openid_list", openidList);
		mapData.put("tagid", to_tagid);
		
		String jsonResult = HttpUtils.post(url, JsonUtils.toJson(mapData));
		return new ApiResult(jsonResult);
	}
	/**
	 * 为单一用户删除标签
	 * @param openid 用户唯一标识符openid
	 * @param tagid 分组id
	 * @return ApiResult
	 */
	public static ApiResult  userBatchDel(String openid, int tagid){
		List<String> openidList = new ArrayList<String>();
		openidList.add(openid);
		ApiResult apiResult = TagApi.batchDelTag(tagid, openidList);
		return apiResult;
	}
}
